/**
 * @file TableTest.cpp
 *
 * This file implements the test for the PAT.
 *
 */

#include "TableTest.h"

#include "cute.h"
#include "PAT.h"
#include "PMT.h"
#include "TS.h"

// Test buffers

unsigned char PATBuffer[] =
{
	0x00, 0xb0, 0x11, 0x00, 0x00, 0xc1, 0x00, 0x00, 0x00, 0x01, 0xe0, 0x20, 0x00, 0x02, 0xe0, 0x21,
	0xf9, 0x62, 0xf5, 0x8b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};

unsigned char PMTBuffer[] =
{
	0x02, 0xb0, 0x1d, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe0, 0x30, 0xf0, 0x00, 0x02, 0xe0, 0x31, 0xf0,
	0x03, 0x52, 0x01, 0x00, 0x81, 0xe0, 0x32, 0xf0, 0x03, 0x52, 0x01, 0x10, 0x20, 0x9a, 0xa3, 0xb8
};

unsigned char TSBuffer[] =
{
	0x47, 0x60, 0x00, 0x10, 0x00, 0x00, 0xb0, 0x0d, 0x00, 0x00, 0xc1, 0x00, 0x00, 0x00, 0x01, 0xe0,
	0x20, 0xf9, 0x62, 0xf5, 0x8b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};


// Test functions

/**
 * @brief Test PAT initialization
 *
 */
void TableTest_PATInit()
{
	PAT_t TestPAT;
	unsigned int Size = sizeof(PATBuffer);

	int Error = PAT_Create(PATBuffer, &Size, &TestPAT);

	ASSERT_EQUALM("Error", 0, Error);
	ASSERT_EQUALM("Size", 20, Size);
	ASSERT_EQUALM("PAT.table_id", 0, TestPAT.table_id);
	ASSERT_EQUALM("PAT.zero", 0, TestPAT.zero);
	ASSERT_EQUALM("PAT.section_length", 0x11, TestPAT.section_length);
	ASSERT_EQUALM("PAT.ProgramDescNumber", 2, TestPAT.ProgramDescNumber);
	ASSERT_EQUALM("PAT.ProgramNumber[0].program_number", 1, TestPAT.program_descriptors[0].program_number);
	ASSERT_EQUALM("PAT.ProgramNumber[0].PID != 0x20", 0x20, TestPAT.program_descriptors[0].PID);
	ASSERT_EQUALM("PAT.ProgramNumber[1].program_number", 2, TestPAT.program_descriptors[1].program_number);
	ASSERT_EQUALM("PAT.ProgramNumber[1].PID", 0x21, TestPAT.program_descriptors[1].PID);
	ASSERT_EQUALM("PAT.CRC_32", 0xf962f58b, TestPAT.CRC_32);
}

/**
 * @brief Test PMT initialization
 *
 */
void TableTest_PMTInit()
{
	PMT_t TestPMT;
	unsigned int Size = sizeof(PMTBuffer);

	int Error = PMT_Create(PMTBuffer, &Size, &TestPMT);

	ASSERT_EQUALM("Error", 0, Error);
	ASSERT_EQUALM("Size", 32, Size);
	ASSERT_EQUALM("PMT.table_id", 2, TestPMT.table_id);
	ASSERT_EQUALM("PMT.zero", 0, TestPMT.zero);
	ASSERT_EQUALM("PMT.section_length", 0x1d, TestPMT.section_length);
	ASSERT_EQUALM("PMT.StreamDescNumber", 2, TestPMT.StreamDescNumber);
	ASSERT_EQUALM("PMT.PCR_PID", 0x30, TestPMT.PCR_PID);
	ASSERT_EQUALM("PMT.CRC_32", 0x209aa3b8, TestPMT.CRC_32);
}


/**
 * @brief Test TS initialization
 *
 */
void TableTest_TSInit()
{
	TS_t TestTS;
	unsigned int Size = sizeof(TSBuffer);

	int Error = TS_Create(TSBuffer, &Size, &TestTS);

	ASSERT_EQUALM("Error", 0, Error);
	ASSERT_EQUALM("Size", TS_SIZE, Size);
	ASSERT_EQUALM("TS.sync_byte", 0x47, TestTS.sync_byte);
	ASSERT_EQUALM("TS.table_id", 0, TestTS.PID);
	ASSERT_EQUALM("TS.adaptation_field_control", 0x1, TestTS.adaptation_field_control);
	ASSERT_EQUALM("TS.DataByteLength", 184, TestTS.DataByteLength);
	ASSERT_EQUALM("TS.data_byte[0]", 0x0, TestTS.data_byte[0]);
}

